package com.ruoyi.web.controller.task;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.TaskInfo;
import com.ruoyi.common.core.domain.model.TaskPage;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;


@RestController
public class TaskController  extends BaseController {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Value("${dts.shop.orderUrl}")
    private String orderUrl;

    @Value("${dts.shop.getTaskUrl}")
    private String getTaskUrl;

    @Value("${dts.shop.addExecuteTaskPicUrl}")
    private String addExecuteTaskPicUrl;

    @Value("${dts.shop.confirmTaskFeeUrl}")
    private String confirmTaskFeeUrl;


    /**
     * 获取推广任务信息
     *
     * @return 推广任务信息
     */
    @PostMapping("getAllUnAcceptedTaskList")
    public AjaxResult getAllUnAcceptedTaskList(@RequestBody TaskPage page)
    {
//        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
//        Set<String> roles = permissionService.getRolePermission(user);
//        // 权限集合
//        Set<String> permissions = permissioXCnService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
//        ajax.put("user", user);
//        ajax.put("roles", roles);
        String param = String.format("?page=%d&limit=%d&sort=id&order=desc&queryType=all", page.getPageNum(), page.getPageSize());
        String response = HttpUtils.sendGet(orderUrl + param);
        JSONObject resultObj = JSON.parseObject(response);
        logger.info("response:{}", response);
        logger.info("total:{}", resultObj.getJSONObject("data").getInteger("total"));

        ajax.put("items", resultObj.getJSONObject("data").getJSONArray("items"));
        ajax.put("total", resultObj.getJSONObject("data").getInteger("total"));
        return ajax;
    }

    @PostMapping("bindTask")
    public AjaxResult bindTask(@RequestBody TaskInfo taskInfo) {

        SysUser user = SecurityUtils.getLoginUser().getUser();
        user.loadExtJson();

        String idCard = user.getExtJson().getString("idCard");
        String tMallNumber = user.getExtJson().getString("tMallNumber");

        String param = String.format("?orderId=%s&userId=%s&userName=%s&nickName=%s&phoneNumber=%s&idCard=%s&tMallNumber=%s"
                ,taskInfo.getOrderId(), user.getUserId(), user.getUserName(), user.getNickName(), user.getPhonenumber(), idCard, tMallNumber);
        String response = HttpUtils.sendGet(getTaskUrl + param);
        JSONObject resultObj = JSON.parseObject(response);
        logger.info("response:{}", response);

        if (Objects.isNull(resultObj) || resultObj.getInteger("errno") == 500) {
            return AjaxResult.error(resultObj.getString("errmsg"));
        }

        AjaxResult ajax = AjaxResult.success();
        return ajax;
    }

    @PostMapping("getOwnerTaskList")
    public AjaxResult getOwnerTaskList(@RequestBody TaskPage page)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
//        Set<String> roles = permissionService.getRolePermission(user);
//        // 权限集合
//        Set<String> permissions = permissioXCnService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
//        ajax.put("user", user);
//        ajax.put("roles", roles);
        String param = String.format("?userId=%d&page=%d&limit=%d&order=desc&queryType=owner&sort=add_time", user.getUserId(), page.getPageNum(), page.getPageSize());
        String response = HttpUtils.sendGet(orderUrl + param);
        JSONObject resultObj = JSON.parseObject(response);
        logger.info("response:{}", response);
        logger.info("total:{}", resultObj.getJSONObject("data").getInteger("total"));

        ajax.put("items", resultObj.getJSONObject("data").getJSONArray("items"));
        ajax.put("total", resultObj.getJSONObject("data").getInteger("total"));
        return ajax;
    }

    @PostMapping("addExecuteTaskPicUrl")
    public AjaxResult addExecuteTaskPicUrl(@RequestBody TaskInfo taskInfo) {

        AjaxResult ajax = AjaxResult.success();
        String param = String.format("?orderId=%s&url=%s", taskInfo.getOrderId(), taskInfo.getPicUrl());
        String response = HttpUtils.sendGet(addExecuteTaskPicUrl + param);
        logger.info("response:{}", response);
        if (StringUtils.isEmpty(response)) {
            return AjaxResult.error();
        }
        return ajax;
    }

    @PostMapping("confirmTaskFee")
    public AjaxResult confirmTaskFee(@RequestBody TaskInfo taskInfo) {

        AjaxResult ajax = AjaxResult.success();
        String param = String.format("?orderId=%s", taskInfo.getOrderId(), taskInfo.getPicUrl());
        String response = HttpUtils.sendGet(confirmTaskFeeUrl + param);
        logger.info("response:{}", response);
        return ajax;
    }

}